﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Management;

namespace dao.daos
{
    public class NadadorDao : BaseDao
    {
        public NadadorDao() : base("nadadores", "id_nadador", true)
        {

        }

        public int ObtenerIdNadador(string nom, string ape)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = StringConexion;
            con.Open();
            DataTable dt = new DataTable();
            string sql =
                "select id_nadador from nadadores where nombre = @nom and apellido = @ape ";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.Add(new SqlParameter("@nom", nom));
            cmd.Parameters.Add(new SqlParameter("@ape", ape));
            dt.Load(cmd.ExecuteReader());
            int idNad = Int32.Parse(dt.Rows[0].ItemArray[0].ToString());
            return idNad;

        }

        public DataTable ObtenerNadadores()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = StringConexion;
            con.Open();
            DataTable dt = new DataTable();
            string sql =
                "select * from nadadores ";
            SqlCommand cmd = new SqlCommand(sql, con);
            dt.Load(cmd.ExecuteReader());
            return dt;

        }

        public DataTable ObtenerPorId(int id)
        {
            Conectar();
            DataTable dt = new DataTable();
            try
            {
                Cmd.CommandText = "SELECT * from nadadores where id_nadador = @id";
                Cmd.Parameters.Add(new SqlParameter("@id", id));
                dt.Load(Cmd.ExecuteReader());
                return dt;

            }
            catch (Exception e)
            {
                throw new SqlExecutionException("Error al obtener club por razón social", e);
            }
            Conexion.Close();
        }

        public int getRows()
        {
            int rows;
            Conectar();
            string query = "Select MAX(id_nadador) FROM nadadores";
            SqlCommand cmd = new SqlCommand(query, Conexion);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            Conexion.Close();
            rows = Int32.Parse(dt.Rows[0].ItemArray[0].ToString());
            return rows;
        }

        public DataTable ObtenerPorNombre(string nombre)
        {
            Conectar();
            DataTable dt = new DataTable();
            try
            {
                Cmd.CommandText = "SELECT * from @nombre_tabla WHERE nombre LIKE @nombre";
                Cmd.Parameters.Add("@nombre_tabla", SqlDbType.VarChar);
                Cmd.Parameters["@nombre_table"].Value = NombreTabla;
                Cmd.Parameters.Add("@nombre", SqlDbType.VarChar);
                Cmd.Parameters["@nombre"].Value = nombre;
                dt.Load(Cmd.ExecuteReader());
                return dt;

            }
            catch (Exception e)
            {
                throw new SqlExecutionException("Error al obtener club por razón social", e);
            }
            Conexion.Close();
        }
    }
}
